package com.liwei.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.javassist.compiler.ast.StringL;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class TestMybatis {

	public static void main(String[] args) throws IOException {
		String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		SqlSession session = sessionFactory.openSession();

		Category category = new Category();




//		fad
//		 Category ct1 = new Category();
//		 ct1.setName("aaa");
//		 session.insert("addCategory", ct1);
//		 System.out.printf("Add %s", ct1);

//		Category getC = session.selectOne("getCategor", 3);
//		System.out.println(getC.getId() + getC.getName());

//		for (int i = 0; i < 5; i++) {
//			Category category = new Category();
//			category.setId(i + 3);
//			category.setName("affad" + (i + 3));
//			session.insert("addCategory", category);
//		}

//		Category c3 = new Category();
//		c3.setId(3);
//		c3.setName("liwei");
//		session.update("updateCategory", c3);

//		Category c3 = new Category();
//		c3.setId(6);
//		c3.setName("liwei");
// 		session.delete("deleteCategory", c3);


		listAll(session);

		session.commit();
		session.close();

	}

	private static void listAll(SqlSession session) {
		List<Category> cs = session.selectList("listCategory");

		for (Category category : cs) {
			System.out.println(category.getName());
		}
	}

}
